﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Interactivity;
using System.ComponentModel;

namespace HugeFlow.OOB.Behaviors
{
    public class OOBDragResizeWindowBehavior : Behavior<FrameworkElement>
    {

        #region WindowResizeEdge

        /// <summary> 
        /// Gets or sets the WindowResizeEdge possible Value of the WindowResizeEdge object.
        /// </summary> 
        public WindowResizeEdge WindowResizeEdge
        {
            get { return (WindowResizeEdge)GetValue(WindowResizeEdgeProperty); }
            set { SetValue(WindowResizeEdgeProperty, value); }
        }

        /// <summary> 
        /// Identifies the WindowResizeEdge dependency property.
        /// </summary> 
        public static readonly DependencyProperty WindowResizeEdgeProperty =
                    DependencyProperty.Register(
                          "WindowResizeEdge",
                          typeof(WindowResizeEdge),
                          typeof(OOBDragResizeWindowBehavior),
                          new PropertyMetadata(WindowResizeEdge.BottomRight, OnWindowResizeEdgePropertyChanged));

        /// <summary>
        /// WindowResizeEdgeProperty property changed handler. 
        /// </summary>
        /// <param name="d">OOBDragResizeWindowBehavior that changed its WindowResizeEdge.</param>
        /// <param name="e">DependencyPropertyChangedEventArgs.</param> 
        private static void OnWindowResizeEdgePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            OOBDragResizeWindowBehavior _OOBDragResizeWindowBehavior = d as OOBDragResizeWindowBehavior;
            if (_OOBDragResizeWindowBehavior != null)
            {
                //TODO: Handle new value. 
            }
        }
        #endregion WindowResizeEdge


        protected override void OnAttached()
        {
            base.OnAttached();

            if (OOBHelper.IsRunningOOBWithPermission == false)
            {
                // 블렌드에서 보고 있지 않으면 컨트롤을 감춘다
                if (DesignerProperties.IsInDesignTool == false)
                {
                    AssociatedObject.Visibility = Visibility.Collapsed;
                }
                return;
            }

            AssociatedObject.MouseLeftButtonDown += new MouseButtonEventHandler(AssociatedObject_MouseLeftButtonDown);
        }

        void AssociatedObject_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Application.Current.MainWindow.DragResize(WindowResizeEdge);
        }

        protected override void OnDetaching()
        {
            base.OnDetaching();

            AssociatedObject.MouseLeftButtonDown -= new MouseButtonEventHandler(AssociatedObject_MouseLeftButtonDown);
        }
    }
}
